/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package UIClass;

import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author Linh-PC
 */
public class SendMail {

    public static void send(String from, String pass,String[] to,String file) throws Exception {
        String host = "smtp.gmail.com";    
        Properties props = System.getProperties();
        props.put("mail.smtp.starttls.enable", "true"); // added this line
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.user", from);
        props.put("mail.smtp.password", pass);
        props.put("mail.smtp.port", "587");
        props.put("mail.smtp.auth", "true");
        Authenticator pa = null;
        final String login = from;
        final String pwd = pass;
        if (login != null && pwd != null) {
            props.put("mail.smtp.auth", "true");
            pa = new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(login, pwd);
                }
            };
        }

      // String[] to = {"muamuaha1122@yahoo.com.vn"}; // added this line

        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        InternetAddress[] toAddress = new InternetAddress[to.length];

// To get the array of addresses
        for (int i = 0; i < to.length; i++) { // changed from a while loop
            toAddress[i] = new InternetAddress(to[i]);
        }
        System.out.println(Message.RecipientType.TO);

        for (int i = 0; i < toAddress.length; i++) { // changed from a while loop
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject("sending in a group");
        MimeMultipart multipart = new MimeMultipart("related");

        // first part  (the html)  
        BodyPart messageBodyPart = new MimeBodyPart();
        String htmlText = "<img src=\"cid:image\">";
        messageBodyPart.setContent(htmlText, "text/html");

        // add it  
        multipart.addBodyPart(messageBodyPart);

        // second part (the image)  
        messageBodyPart = new MimeBodyPart();
        DataSource fds = new FileDataSource(file);
        messageBodyPart.setDataHandler(new DataHandler(fds));
        messageBodyPart.setHeader("Content-ID", "<image>");

        // add it  
        multipart.addBodyPart(messageBodyPart);

        // put everything together  
        message.setContent(multipart);

// — Set some other header information –
        message.setHeader("X-Mailer", "LOTONtechEmail");
        message.setSentDate(new Date());
        message.saveChanges();
// — Send the message –
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
       
    }
    /*  public static void send(String smtpServer, String to, String from, String psw,
     String subject, String file) throws Exception {
     // Code provide by congdongjava.com
     Properties props = System.getProperties();
     props.put("mail.smtp.host", smtpServer);
     props.put("mail.smtp.port", "587");
     props.put("mail.smtp.starttls.enable", "true");
     String[] to1 = {"to@gmail.com"}; // added this line
     final String login = from;
     final String pwd = psw;
     Authenticator pa = null;
     if (login != null && pwd != null) {
     props.put("mail.smtp.auth", "true");
     pa = new Authenticator() {

     @Override
     public PasswordAuthentication getPasswordAuthentication() {
     return new PasswordAuthentication(login, pwd);
     }
     };
     }//else: no authentication
     Session session = Session.getInstance(props, pa);
     // — Create a new message –
     Message msg = new MimeMessage(session);
     // — Set the FROM and TO fields –
     msg.setFrom(new InternetAddress(from));
     msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
     to, false));

     // — Set the subject and body text –
     msg.setSubject(subject);
       
     MimeMultipart multipart = new MimeMultipart("related");  
  
     // first part  (the html)  
     BodyPart messageBodyPart = new MimeBodyPart();  
     String htmlText = "<img src=\"cid:image\">";  
     messageBodyPart.setContent(htmlText, "text/html");  
  
     // add it  
     multipart.addBodyPart(messageBodyPart);  
          
     // second part (the image)  
     messageBodyPart = new MimeBodyPart();  
     DataSource fds = new FileDataSource(file);  
     messageBodyPart.setDataHandler(new DataHandler(fds));  
     messageBodyPart.setHeader("Content-ID","<image>");  
  
     // add it  
     multipart.addBodyPart(messageBodyPart);  
  
     // put everything together  
     msg.setContent(multipart);  
                
     // — Set some other header information –
     msg.setHeader("X-Mailer", "LOTONtechEmail");
     msg.setSentDate(new Date());
     msg.saveChanges();
     // — Send the message –
     Transport.send(msg);
     // System.out.println("Mail da duoc gui");
     JOptionPane.showMessageDialog(null, "gửi mail thành công", "send mail", 1);
     }
     * */
}
